package com.hanson.service;

import com.hanson.common.util.BusinessUtil;
import com.hanson.entity.GoodsScoreBean;
import com.hanson.mapper.GoodsBeanMapper;
import com.hanson.mapper.GoodsScoreBeanMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GoodsScoreService {
    @Autowired
    private GoodsScoreBeanMapper goodsScoreBeanMapper;
    @Autowired
    private GoodsBeanMapper goodsBeanMapper;

    public GoodsScoreBean getGoodsScore(int goodsId) {
        List<GoodsScoreBean> list = goodsScoreBeanMapper.selectByGoodsId(goodsId);
        if (CollectionUtils.isNotEmpty(list)) {
            GoodsScoreBean goodsScoreBean = list.get(0);
            goodsScoreBean.setMoney((int) Double.parseDouble(BusinessUtil.getWebAmount(goodsScoreBean.getMoney())));
            return goodsScoreBean;
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    public void setGoodsScore(GoodsScoreBean goodsScore) {
        List<GoodsScoreBean> goodsScoreBeanList = goodsScoreBeanMapper.selectByGoodsId(goodsScore.getGoodsId());
        if (goodsScore.getIsOpen() == 0) {
            //关闭积分兑换
            if (CollectionUtils.isNotEmpty(goodsScoreBeanList)) {
                GoodsScoreBean goodsScoreBean = goodsScoreBeanList.get(0);
                goodsScoreBean.setIsOpen(0);
                goodsScoreBean.setType(goodsScore.getType());
                if (null != goodsScore.getMoney()) {
                    goodsScoreBean.setMoney(BusinessUtil.getDbAmount(goodsScore.getMoney() + ""));
                } else {
                    goodsScoreBean.setMoney(0);
                }
                goodsScoreBean.setScore(null == goodsScore.getScore() ? 0 : goodsScore.getScore());
                goodsScoreBean.setUpdateTime(new Date());
                goodsScoreBeanMapper.updateByPrimaryKey(goodsScoreBean);
            }
            goodsBeanMapper.updateIsScoreById(0, goodsScore.getGoodsId());
        } else {
            //开通积分兑换
            if (CollectionUtils.isNotEmpty(goodsScoreBeanList)) {
                GoodsScoreBean goodsScoreBean = goodsScoreBeanList.get(0);
                goodsScoreBean.setIsOpen(1);
                goodsScoreBean.setType(goodsScore.getType());
                if (null != goodsScore.getMoney()) {
                    goodsScoreBean.setMoney(BusinessUtil.getDbAmount(goodsScore.getMoney() + ""));
                } else {
                    goodsScoreBean.setMoney(0);
                }
                goodsScoreBean.setScore(null == goodsScore.getScore() ? 0 : goodsScore.getScore());
                goodsScoreBean.setUpdateTime(new Date());
                goodsScoreBeanMapper.updateByPrimaryKey(goodsScoreBean);
            } else {
                goodsScore.setCreateTime(new Date());
                goodsScoreBeanMapper.insert(goodsScore);
            }
            goodsBeanMapper.updateIsScoreById(1, goodsScore.getGoodsId());
        }
    }
}
